/**
 * @author yinyuejia
 * @file 多语言存入vuex & storage
 */

import storage from '@/utils/storage';
import i18n, { setI18nLanguage } from '@/locale';

const defaultState = {
  key: 'en-US', // 默认语言
};
const storageKey = 'locale';

export default {
  state: defaultState,
  mutations: {
    // 读取本地看看有没有缓存
    reload(state: any) {
      const data = storage.get(storageKey);
      if (data) {
        Object.keys(data).forEach((k) => {
          state[k] = data[k];
        });
        setI18nLanguage(i18n, state.key);
      }
    },
    clear() {},
    // 设置语言
    setLocale(state: any, key: any) {
      state.key = key;
      setI18nLanguage(i18n, key);
      storage.set(`${storageKey}.key`, state.key);
    },
  },
};
